function datac2bin()
%
% convert datac files to simple binary, throwing away ALL information
% except for the data itself. You can then read the data into clampex
% with a binary read (NO HEADER INFORMATION!).

lookpath='F:\data\pok\hippo\*.pok';
[filename, path] = uigetfile(lookpath, 'Open Data Files');
if(filename == 0)
	fprintf('Unable to access file? %s - %s', path, filename);
   err = 1;
   return;
end
truefile = strcat(path, filename);

[DFILE, data, err] = datac2mat(truefile, []); % just read the header - empty RL list
RL = 1:DFILE.records_in_file; % makes us read them ALL

fprintf('converting %d records\n', length(RL));

fidout = fopen('test.dat', 'w');
if(fidout == 0)
   fprintf('Could''nt open test.dat output file');
   return;
end;


for i=RL
   [DFILE, data, err] = datac2mat(truefile, i);
   MASK = hex2dec('fff');	% lower bit mask for the logic information
   ZERO  = 2048;
   % intialize the output arguments.
   current=[];
   voltage=[];
   wchan=[];
   
   %reference gains
   pts=DFILE.nr_points;
   
   if(DFILE.mode == 0 | DFILE.mode == 1)
      %voltage cha 1
      voltage(1,1:pts)  = (bitand(floor(data(1,1:pts)), MASK) - ZERO)/refvg*MB_CAL/DFILE.vgain+DFILE.junctionpot;	
      %current cha 2 in nA (thats why /1000)
      current(1,1:pts)  = (bitand(floor(data(1,pts+1:2*pts)), MASK) - ZERO)/refig*MB_CAL/DFILE.igain;	
      %channel 3 gain
      if(DFILE.nr_channel == 3) wchan(1,1:pts) = (bitand(floor(data(1,2*pts+1:3*pts)), MASK) - ZERO)*MB_CAL/DFILE.wgain;	
      end
   end
   
   
   if(DFILE.mode == 3)
      %voltage cha 1
      voltage(1,1:pts)  = floor(data(1,1:pts)/16);	
      %current cha 2 in nA (thats why /1000)
      current(1,1:pts)  = floor(data(1,pts+1:2*pts)/16);	
      %channel 3 gain
      if(DFILE.nr_channel == 3) wchan(1,1:pts) = floor(data(1,2*pts+1:3*pts)/16);	
      end
   end
   
   if(DFILE.mode == 2)
      %voltage cha 1
      voltage(1,1:pts)  = (bitand(floor(data(1,1:pts)), MASK) - ZERO);	
      %current cha 2 in nA (thats why /1000)
      current(1,1:pts)  = (bitand(floor(data(1,pts+1:2*pts)), MASK) - ZERO);	
      %channel 3 gain
      if(DFILE.nr_channel == 3) wchan(1,1:pts) = (bitand(floor(data(1,2*pts+1:3*pts)), MASK) - ZERO);	
      end
   end
   
   odat(1,1:2:2*pts-1) = floor(data(1,1:pts))/16;
   odat(1,2:2:2*pts) = floor(data(1,pts+1:2*pts))/16;
   
   fwrite(fidout, odat, 'integer*2');
 %  fwrite(fidout, current(1,:), 'integer*2');
end;
fclose(fidout);


